package com.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.bean.Activity;
import com.club.bean.ActivityParticipation;
import com.club.bean.Administrator;
import com.club.service.ActivityParticipationService;
import com.club.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("activityparticipation")
@CrossOrigin
public class ActivityParticipationController {
    @Autowired
    ActivityParticipationService activityParticipationService;
    @GetMapping("list")
    public Result getList(){
        List<ActivityParticipation> list=activityParticipationService.list();
        return Result.success(list);
    }
    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(value = "searchText",required = false) String searchText){
        System.out.println(searchText);
        IPage<ActivityParticipation> result=null;
        if(searchText==null||searchText.trim().isEmpty()) {
            result=activityParticipationService.page(page);
        }else{
            QueryWrapper<ActivityParticipation> wrapper= new QueryWrapper<>();
            wrapper.like("partId",searchText)
                    .or().like("actId",searchText)
                    .or().like("stuId",searchText)
                    .or().like("partTime",searchText);
            result=activityParticipationService.page(page,wrapper);
        }
        return Result.success(result);
    }
    @PostMapping("add")
    public Result add(@RequestBody ActivityParticipation actpart){
        activityParticipationService.save(actpart);
        ActivityParticipation actpart1=activityParticipationService.getById(actpart.getPartId());
        return Result.success("保存成功",actpart);
    }
    //删除管理员
    @DeleteMapping("/{partId}")
    public Result delete(@PathVariable("partId") Integer partId){
        boolean b=activityParticipationService.removeById(partId);
        if(b){
            return Result.success("删除成功");
        }else{
            return Result.error(201,"删除失败");
        }
    }
    @PutMapping("edit")
    //修改管理员信息
    public Result edit(@RequestBody ActivityParticipation actpart){
        boolean b= activityParticipationService.saveOrUpdate(actpart);
        //保存管理员信息
        ActivityParticipation actpart1=activityParticipationService.getById(actpart.getPartId());
        if(b){
            return Result.success("修改成功",actpart1);
        }else {
            return Result.error(201,"修改失败");
        }
    }
}
